package com.infore.airsupervise.biz.impl;


import com.alibaba.fastjson.JSON;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.LoginBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/3/21 15:42
 */
public class LoginBizImpl implements LoginBiz {
    @Override
    public Call<BaseResponse> sendUserLoginRequest(String username, String passWord, final OnServerResponseListener<UserE> listener) {
        Map<String ,Object> params = new HashMap<>();
        params.put("account",username);
        params.put("password",passWord);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.USER_LOGIN,params);
        call.enqueue( new HttpCallBack() {
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                UserE userE= JSONUtil.parseBean(response.getData(), Constants.RESPONSE_BEAN_KEY,UserE.class);
                listener.onSucceed(userE,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, desc ,"用户名或密码错误" );
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode,title,desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> getVideoTokenReuqest(final OnServerResponseListener<String> listener) {

        Call<BaseResponse>  call = HttpUtil.getHttpService().
                postParams(HttpAPI.GET_VIDEO_TOKEN,Collections.EMPTY_MAP);
        call.enqueue( new HttpCallBack() {
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                String token = JSON.parseObject(response.getData().get(Constants.RESPONSE_BEAN_KEY).toString()).getString("accessToken");
                listener.onSucceed(token,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode,MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return  call;
    }
}
